package top.shiyiri.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author Aunean
 * @date 2022/1/17 13:15
 * @Description 操作数据库的工具类
 */
public class JDBCUtils {

    public static Connection getConnection() throws Exception{
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        Properties properties = new Properties();
        properties.load(inputStream);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");

        //2.加载驱动
        Class.forName(driverClass);

        //3.获取连接
        return DriverManager.getConnection(url, user, password);
    }

    public static void closeResource(Connection conn, Statement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
        closeResource(conn, ps);
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description 使用DBCP数据库连接池技术获取数据库连接
     */
    private static DataSource source;

    static {
        InputStream is = null;
        try {
            Properties pros = new Properties();
            is = ClassLoader.getSystemResourceAsStream("dbcp.properties");
            //创建一个DBCP数据库连接池
            pros.load(is);
            source = BasicDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           if (is != null) {
               try {
                   is.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }

    public static Connection getDataSource() throws SQLException {
        return source.getConnection();
    }


    //获取C3P0数据库连接池，数据库连接池只提供一个即可
    private static ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");

    public static Connection getConnection2() throws SQLException {
        return cpds.getConnection();
    }
    

    private static InputStream is = null;
    private static DataSource source2 = null;
    static {
        try {
            Properties pros = new Properties();
            is = ClassLoader.getSystemResourceAsStream("druid.properties");
            pros.load(is);
            source2 = DruidDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert is != null;
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static Connection getConnection3() throws SQLException {
        return source2.getConnection();
    }

    /**
     * @Description 使用 dbutils中提供的DbUtils工具类，实现资源的关闭
     */
    public static void closeResourceUtils(Connection conn, Statement stmt, ResultSet rs) {
        DbUtils.closeQuietly(conn, stmt, rs);
    }

    public static void closeResourceUtils(Connection conn, Statement stmt) {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(stmt);
    }

}
